﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Currency.TextUtils;
using CatUtils.Utils.Enhance.ThreadTool;
using CatUtils.Utils.SpecialUtils.Html;
using CatUtilsDll.Utils.Currency.WebUtils;
using RabbitDownload.Helper;
using RabbitDownload.Helper.Currency;

using RabbitDownload.Helper.Enhance;
using System;
using System.Collections.Generic;

namespace RabbitDownload.WebSite.Ehentai
{
    internal class EHentaiScanner
    {
        public static void Start(string StartPage, string EndPage, string Score, string API, bool Sleep)
        {
            int StartCount = TextHelper.StringToInt(StartPage);
            int EndCount = TextHelper.StringToInt(EndPage);
            int ScoreCount = TextHelper.StringToInt(Score);
            if (EndCount < StartCount || StartCount == 0)
            {
                Console.WriteLine("起始页小于结束页");
                return;
            }
            List<string> ArticleList = GetArticleList(StartCount, EndCount, ScoreCount, API);
            ArticleListDownLoad(ArticleList, Sleep);
        }

        public void SaveArticleList(string StartPage, string EndPage, string Score, string API)
        {
            int StartCount = TextHelper.StringToInt(StartPage);
            int EndCount = TextHelper.StringToInt(EndPage);
            int ScoreCount = TextHelper.StringToInt(Score);
            List<string> ArticleList = GetArticleList(StartCount, EndCount, ScoreCount, API);
            string SaveFilePath = StaticValueOld.SaveListPath + "E站文章列表.txt";
            FileHelper.WriteList(SaveFilePath, ArticleList);
            Console.WriteLine("保存在：" + SaveFilePath);
        }

        public void DownloadListFile(string FilePath)
        {
            List<string> ArticleList = FileHelper.ReadAllLine(FilePath);

            List<string> TempList = FileHelper.ReadAllLine(FilePath);
            int i = 0;
            foreach (string line in ArticleList)
            {
                TempList.RemoveAt(i++);
                FileHelper.WriteList(FilePath, TempList);
                if (line == null)
                    continue;
                ThreadSleep.RandomSecondClock(300, 500);

                SingLeArticleDownload(line);
            }
        }

        /// <summary>
        /// 获取所有文章列表
        /// </summary>
        /// <param name="StartCount"></param>
        /// <param name="EndCount"></param>
        /// <param name="API"></param>
        /// <returns></returns>
        private static List<string> GetArticleList(int StartCount, int EndCount, int ScoreCount, string API)
        {
            List<string> ArticleList = new List<string>();
            for (int i = StartCount; i <= EndCount; i++)
            {
                String ScannerLink = string.Format(API, i);
                Console.WriteLine(ScannerLink);
                String Shtml = WebHelper.HttpPcGet(ScannerLink);
                foreach (string line in HtmlHelper.GetHtmlValueList(Shtml, "//table[@class='itg']/tr"))
                {
                    string Link = HtmlHelper.GetFirstLink(line, "//div[@class='it5']/a");

                    string Style = HtmlHelper.GetFirstAttributes(line, "style", "//div[@class='it4']/div");

                    if (Link == "" || Style == "")
                    {
                        Console.WriteLine("未获取到，跳过 ");
                        continue;
                    }
                    int Score = new EHentaiScanner().EhentaiGetScore(Style);
                    Console.WriteLine("预设{0}，获取到{1}", ScoreCount, Score);
                    if (Score >= ScoreCount)
                    {
                        ArticleList.Add(Link);
                        Console.WriteLine("符合条件,已存储{0}个任务。", ArticleList.Count);
                    }
                }
            }
            return ArticleList;
        }

        /// <summary>
        /// 获取E站评分
        /// </summary>
        /// <param name="style"></param>
        /// <returns></returns>
        private int EhentaiGetScore(string Style)
        {
            //background-position:-48px -1px; opacity:1
            string RepText = TextHelper.RegexText(Style, "background-position:(-|)[0-9]{1,2}");
            RepText = TextHelper.ReplaceTextEnhance(RepText, "background-position:(-|)", "");
            switch (TextHelper.StringToInt(RepText))
            {
                case 64:
                    return 1;

                case 48:
                    return 2;

                case 32:
                    return 3;

                case 16:
                    return 4;

                case 0:
                    return 5;

                default:
                    return 0;
            }
        }

        /// <summary>
        /// 下载列表
        /// </summary>
        /// <param name="articleList"></param>
        /// <param name="Sleep"></param>
        private static void ArticleListDownLoad(List<string> articleList, bool Sleep)
        {
            foreach (string line in articleList)
            {
                if (Sleep)
                {
                    ThreadSleep.RandomSecondClock(300, 500);
                }

                SingLeArticleDownload(line);
            }
        }

        /// <summary>
        /// 下载单个
        /// </summary>
        /// <param name="ArticleLink"></param>
        public static void SingLeArticleDownload(string ArticleLink)
        {
            List<string> ImgList = new List<string>();
            String Shtml = WebHelper.HttpPcGet(ArticleLink);
            String Title = "";

            foreach (string line in HtmlHelper.GetTextValueList(Shtml, "//div[@id='gd2']/h1"))
            {
                Console.WriteLine(line);
                if (Title != "")
                    Title += string.Format("-{0}", line);
                else
                    Title += line;
            }
            Console.WriteLine("文章标题" + Title);
            if (EhentaiHelper.PreviewMode)
                ImgList = GetPreviewImg(ImgList, Shtml);
            else
                ImgList = GetAllImg(ImgList, Shtml);
            WebSite.WebCurrency.SaveHelper.AutoSave(ImgList, "E站", Title);
        }

        private static List<string> GetPreviewImg(List<string> ImgList, string Shtml)
        {
            int Count = EhentaiHelper.PreviewModeCount;
            foreach (string line in HtmlHelper.GetLinkVlaueList(Shtml, "//div[@class='gdtm']/div/a"))
            {
                if (Count-- == 0)
                {
                    Console.WriteLine("预览模式结束，跳出");
                    break;
                }
                String ImgLink = GetImg(line);
                Console.WriteLine("图片链接" + ImgLink);
                if (ImgLink != "")
                    ImgList.Add(ImgLink);
            }
            return ImgList;
        }

        private static List<string> GetAllImg(List<string> ImgList, string Shtml)
        {
            foreach (string line in HtmlHelper.GetLinkVlaueList(Shtml, "//div[@class='gdtm']/div/a"))
            {
                String ImgLink = GetImg(line);
                Console.WriteLine("图片链接" + ImgLink);
                if (ImgLink != "")
                    ImgList.Add(ImgLink);
            }
            return ImgList;
        }

        /// <summary>
        /// 解析单页，获取链接
        /// </summary>
        /// <param name="line"></param>
        private static string GetImg(string ImgPageUrl)
        {
            String Shtml = WebHelper.HttpPcGet(ImgPageUrl);

            return HtmlHelper.GetFirstSrc(Shtml, "//div[@id='i3']/a/img");
        }
    }
}